﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using ERP.Manager.DataEntity;
using ERP.Manager.BusinessServices;
using ERP.Manager.Controllers.CustomAttributes;
namespace ERP.Manager.Controllers
{
    [CheckLoginAttribute]
    public class ECS_goods_typeController : Controller
    {
        private readonly ECS_goods_typeinfoService ecs_goods_typeinfoservice;
        private readonly ECS_attributeinfoService ecs_attributeinfoService;
        public ECS_goods_typeController() { }
        public ECS_goods_typeController(ECS_goods_typeinfoService ecs_goods_typeinfoservices, ECS_attributeinfoService ecs_attributeinfoservices)
        {
            this.ecs_goods_typeinfoservice = ecs_goods_typeinfoservices;
            this.ecs_attributeinfoService = ecs_attributeinfoservices;
        }
        #region 显示商品类型页面
        public ActionResult ShowECS_goods_type(int? page)
        {
            int pagecount = page ?? 1;
            List<ECS_goods_typeinfo> list = ecs_goods_typeinfoservice.all_ecs_goods_typeinfo_page(pagecount);
            ViewData["ecs_goods_typeinfo"] = list;
            ViewData["ecs_goods_typeinfo_count"] = ecs_goods_typeinfoservice.all_ecs_goods_typeinfo_count();
            return View();
        }
        #endregion

        #region 添加商品类型页面
        public ActionResult AddECS_goods_type()
        {
            return View();
        }
        #endregion

        #region Ajax添加商品类型
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public string AjaxAddECS_goods_type(string cat_name, string attr_group)
        {
            try
            {
                ECS_goods_typeinfo goodstype = new ECS_goods_typeinfo();
                goodstype.cat_name = cat_name;
                goodstype.attr_group = attr_group;
                goodstype.cat_enabled = 1;
                int rowcount = ecs_goods_typeinfoservice.add_ecs_goods_typeinfo(goodstype);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }
        }
        #endregion

        #region 修改商品类型页面
        public ActionResult UpdateECS_goods_type(string cat_id)
        {
            ECS_goods_typeinfo goodstype = ecs_goods_typeinfoservice.detail_ecs_goods_typeinfo(int.Parse(cat_id.ToString()));
            ViewData["ecs_goods_typeinfo"] = goodstype;
            return View();
        }
        #endregion

        #region Ajax修改商品类型
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxUpdateECS_goods_type(string cat_id, string cat_name, string attr_group)
        {
            try
            {
                ECS_goods_typeinfo goodstype = new ECS_goods_typeinfo();
                goodstype.cat_id = int.Parse(cat_id);
                goodstype.cat_name = cat_name;
                goodstype.attr_group = attr_group;
                int rowcount = ecs_goods_typeinfoservice.update_ecs_goods_typeinfo(goodstype);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }
        }
        #endregion

        #region ajax删除商品类型
        public string AjaxDeleteECS_goods_typeinfo(int cat_id)
        {
            try
            {
                int count = ecs_attributeinfoService.all_ecs_attributeinfo_cat_id_count(cat_id);
                if (count != 0)
                {
                    return "error";
                }
                else
                {
                    int rowcount = ecs_goods_typeinfoservice.delete_ecs_goods_typeinfo(cat_id);
                    if (rowcount > 0)
                    {
                        return "success";
                    }
                    else
                    {
                        return "error";
                    }
                }
            }
            catch (Exception)
            {
                return "抱歉，删除商品类型失败！";
            }
        }
        #endregion

        #region 商品类型属性列表页面
        public ActionResult ShowECS_attribute(int cat_id, int? page)
        {
            Session.Add("cat_id", cat_id);
            int pagecount = page ?? 1;
            List<ECS_attributeinfo> list = ecs_attributeinfoService.all_ecs_attributeinfo_page(cat_id, pagecount);
            ViewData["ecs_attributeinfo"] = list;
            ViewData["all_ecs_attributeinfo_cat_id_count"] = ecs_attributeinfoService.all_ecs_attributeinfo_cat_id_count(cat_id);
            return View();
        }
        #endregion

        #region 添加商品类型属性页面
        public ActionResult AddECS_attribute()
        {
            List<ECS_goods_typeinfo> list = ecs_goods_typeinfoservice.all_ecs_goods_typeinfo();
            ViewData["ecs_goods_typeinfo"] = list;
            return View();
        }
        #endregion

        #region ajax查询商品类型的属性
        public string AjaxECS_attributeinfo_attr_group(string cat_id)
        {
            string html = "";
            int attr_group_id = 1;
            ECS_goods_typeinfo goodstype = ecs_goods_typeinfoservice.detail_ecs_goods_typeinfo(int.Parse(cat_id.ToString()));
            string attr_group = goodstype.attr_group;
            if (attr_group == "")
            {
                html += "";
            }
            else
            {
                string[] attr = attr_group.Split('\n');

                for (int i = 0; i < attr.Length; i++)
                {
                    html += "<option value=" + attr_group_id++ + ">" + attr[i] + "</option>";
                }
            }
            return html;
        }
        #endregion

        #region 添加商品类型属性页面
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public string AjaxAddECS_attribute(string cat_id, string attr_name, string attr_input_type, string attr_type, string attr_values, string attr_index, string is_linked, string attr_group)
        {
            try
            {
                ECS_attributeinfo attribute = new ECS_attributeinfo();
                attribute.ecsgoodstypeinfo.cat_id = int.Parse(cat_id);
                attribute.attr_name = attr_name;
                attribute.attr_input_type = int.Parse(attr_input_type);
                attribute.attr_type = int.Parse(attr_type);
                attribute.attr_values = attr_values == null ? "" : attr_values;
                attribute.attr_index = int.Parse(attr_index);
                attribute.sort_order = 0;
                attribute.is_linked = int.Parse(is_linked);
                attribute.attr_group = int.Parse(attr_group);
                int rowcount = ecs_attributeinfoService.add_ecs_attrbuteinfo(attribute);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }
        }
        #endregion

        #region 修改商品类型属性页面
        public ActionResult UpdateECS_attribute(string attr_id)
        {
            List<ECS_goods_typeinfo> list = ecs_goods_typeinfoservice.all_ecs_goods_typeinfo_cat_name();
            ViewData["ecs_goods_typeinfo"] = list;
            ECS_attributeinfo attribute = ecs_attributeinfoService.detail_ecs_attributeinfo(int.Parse(attr_id.ToString()));
            ViewData["ecs_attributeinfo"] = attribute;
            ECS_goods_typeinfo ecs_goods_typeinfo = ecs_goods_typeinfoservice.detail_ecs_goods_typeinfo(attribute.ecsgoodstypeinfo.cat_id);
            ViewData["detail_ecs_goods_typeinfo"] = ecs_goods_typeinfo;
            return View();
        }
        #endregion

        #region Ajax修改商品类型属性页面
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public string AjaxUpdateECS_attribute(string attr_id, string cat_id, string attr_name, string attr_input_type, string attr_type, string attr_values, string attr_index, string is_linked, string attr_group)
        {
            try
            {
                ECS_attributeinfo attribute = new ECS_attributeinfo();
                attribute.attr_id = int.Parse(attr_id);
                attribute.ecsgoodstypeinfo.cat_id = int.Parse(cat_id);
                attribute.attr_name = attr_name;
                attribute.attr_input_type = int.Parse(attr_input_type);
                attribute.attr_type = int.Parse(attr_type);
                attribute.attr_values = attr_values == null ? "" : attr_values;
                attribute.attr_index = int.Parse(attr_index);
                attribute.is_linked = int.Parse(is_linked);
                attribute.attr_group = int.Parse(attr_group);
                int rowcount = ecs_attributeinfoService.update_ecs_attributeinfo(attribute);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }
        }
        #endregion

        #region Ajax删除商品类型属性
        public string AjaxDeleteECS_attributeinfo(string attr_id)
        {
            int cat_id = (int)Session["cat_id"];
            try
            {
                int rowcount = ecs_attributeinfoService.delete_ecs_attributeinfo(int.Parse(attr_id.ToString()));
                if (rowcount > 0)
                {
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion
    }
}
